package com.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试控制器
 * 用于验证数据库连接和表结构
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 测试数据库连接
     * 执行一个简单查询，查看当前连接的数据库信息
     */
    @GetMapping("/db-info")
    public Map<String, Object> getDbInfo() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 查询当前连接的数据库信息
            String dbName = jdbcTemplate.queryForObject("SELECT DATABASE()", String.class);
            String version = jdbcTemplate.queryForObject("SELECT VERSION()", String.class);
            
            log.info("数据库连接测试 - 数据库: {}, 版本: {}", dbName, version);
            
            result.put("success", true);
            result.put("database", dbName);
            result.put("version", version);
        } catch (Exception e) {
            log.error("数据库连接测试失败", e);
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 验证表结构
     */
    @GetMapping("/table-info")
    public Map<String, Object> getTableInfo() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 检查表是否存在
            String checkTableSql = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 't_user'";
            Integer tableCount = jdbcTemplate.queryForObject(checkTableSql, Integer.class);
            
            if (tableCount > 0) {
                // 获取表结构
                String descTableSql = "DESCRIBE t_user";
                List<Map<String, Object>> tableStructure = jdbcTemplate.queryForList(descTableSql);
                
                // 获取表中的数据数量
                String countSql = "SELECT COUNT(*) FROM t_user";
                Integer dataCount = jdbcTemplate.queryForObject(countSql, Integer.class);
                
                result.put("success", true);
                result.put("tableExists", true);
                result.put("tableStructure", tableStructure);
                result.put("dataCount", dataCount);
                
                log.info("表结构验证成功 - 表存在，数据数量: {}", dataCount);
            } else {
                result.put("success", true);
                result.put("tableExists", false);
                log.warn("表结构验证 - t_user表不存在");
            }
        } catch (Exception e) {
            log.error("表结构验证失败", e);
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 直接执行插入测试
     */
    @GetMapping("/direct-insert")
    public Map<String, Object> directInsert() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 直接执行插入SQL
            String insertSql = "INSERT INTO t_user (id, username, password, age, email) VALUES (?, ?, ?, ?, ?)";
            Long testId = System.currentTimeMillis() % 10000000000L; // 生成10位测试ID
            
            int rows = jdbcTemplate.update(insertSql, testId, "直接插入测试", "123456", 30, "direct@test.com");
            
            log.info("直接插入测试 - 影响行数: {}, 测试ID: {}", rows, testId);
            
            // 验证插入结果
            String selectSql = "SELECT * FROM t_user WHERE id = ?";
            List<Map<String, Object>> selectResult = jdbcTemplate.queryForList(selectSql, testId);
            
            result.put("success", true);
            result.put("insertedRows", rows);
            result.put("testId", testId);
            result.put("selectResult", selectResult);
            result.put("verified", !selectResult.isEmpty());
            
        } catch (Exception e) {
            log.error("直接插入测试失败", e);
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return result;
    }
} 